﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;

namespace IHISCETeim.Web.MachineSys.UserModel
{
    public partial class UClaimDemageControlEdit : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.修改,Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get { return "UClaimDemageControlList"; }
        }


        protected string ClaimsNumber
        {
            get
            {
                return GetRequestValue<string>("ClaimsNumber", "");
            }
        }

        private UClaimDemageControlInfo _obj;
        protected UClaimDemageControlInfo obj
        {
            get
            {
                if (_obj == null)
                {
                    _obj = UClaimDemageControlController.GetByClaimsNumber(ClaimsNumber);                   
                }
                return _obj;              
            }
        }
        private DataTable _SupplerDetailDT;
        protected DataTable SupplerDetailDT
        {
            get
            {
                if (_SupplerDetailDT == null)
                {
                    _SupplerDetailDT = UClaimDemageController.GetByClaimsNumber(ClaimsNumber);
                }
                return _SupplerDetailDT;
            }
        }

        protected string GetIntoDate()
        {
            return  UWarrantyCardController.GetBySerialNumber(obj.SerialNumber).IntoDate.ToString("yyyy-MM-dd");
        }

        protected string GetDutyPanDing(int index)
        {
            obj.DutyPanDing = string.IsNullOrEmpty(obj.DutyPanDing) ? "" : obj.DutyPanDing;
           string[] DutyPanDingArr= obj.DutyPanDing.Split(new char[] { ',' });
           index = index - 1;
           if (DutyPanDingArr.Count() == 0)
               return "";
           if (DutyPanDingArr.Count() > index)
               return DutyPanDingArr[index];
           else
               return "";
        }

        protected string GetDutyPanDingSelect(string selectvalue)
        {
            string[] strarr = new string[] { "厂内责任", "营业政策性索赔", "特殊索赔约定", "其它", "专案件", "试制件", "服务索赔", "服务政策性索赔" };
            string optemp = "<option value=\"{0}\" {1}>{0}</option>";
            string rstr = string.Format(optemp, "请选择....", selectvalue == "" ? "selected" : ""); 
            foreach (string t in strarr)
            {
                rstr += string.Format(optemp, t, selectvalue == t ? "selected" : "");
            }
            return rstr;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "edit")
            {
                addmenu();
                
            }

            this.rpAgentQltyParts.DataSource = AgentQltyController.GetAllPartsByClaimsNumber(obj.ClaimsNumber);
            this.rpAgentQltyParts.DataBind();

            this.rpAnalyse.DataSource = SupplerDetailDT;
            this.rpAnalyse.DataBind();

            rpOITMDetail.DataSource = UClaimDemageControlOITMDetailController.GetListByUCDCID(obj.UCDCID);
            rpOITMDetail.DataBind();
        }
        private void addmenu()
        {
            UClaimDemageControlInfo obj2 = Request.SerializableObject<UClaimDemageControlInfo>();

            //处理原材料号码 2013-02-04
            char[] caSplit = new char[] { ',' };
            string[] ItemCodes = GetRequestValue<string>("DetailItemCode", "").Split(caSplit);
            string[] Quantitys = GetRequestValue<string>("DetailQuantity", "").Split(caSplit);
            List<UClaimDemageControlOITMDetailInfo> list = new List<UClaimDemageControlOITMDetailInfo>();
            for (int index = 0; index < ItemCodes.Count(); index++)
            {
                string itemcode = ItemCodes[index];
                if (!string.IsNullOrEmpty(itemcode.Trim()))
                {
                    decimal Quantity = Convert.ToDecimal(Quantitys[index]);

                    UClaimDemageControlOITMDetailInfo detailObj = new UClaimDemageControlOITMDetailInfo();
                    detailObj.ItemCode = itemcode;
                    detailObj.ItemName = SBOController.GetHaveItemCode(itemcode);
                    detailObj.Quantity = Quantity;

                    list.Add(detailObj);
                }
            }

            this.AddLog(Permission.修改,obj, obj2);

            bool r = UClaimDemageControlController.Update(obj2);
            if (r)
            {
                UClaimDemageControlOITMDetailController.AddUClaimDemageControlOITMDetail(obj2.UCDCID, list);
                SuccessAlertJson("修改成功");
            }
            else
            {
                SuccessAlertJson("修改失败");
            }
        }
    }
}